home *** CD-ROM | disk | FTP | other *** search
/ The CICA Windows Explosion! / The CICA Windows Explosion! - Disc 2.iso / misc / gs261src.zip / zstack.c < prev    next >
C/C++ Source or Header  |  1993-05-22  |  5KB  |  206 lines

  1. /* Copyright (C) 1989, 1991, 1992 Aladdin Enterprises.  All rights reserved.
  2.  
  3. This file is part of Ghostscript.
  4.  
  5. Ghostscript is distributed in the hope that it will be useful, but
  6. WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  7. to anyone for the consequences of using it or for whether it serves any
  8. particular purpose or works at all, unless he says so in writing.  Refer
  9. to the Ghostscript General Public License for full details.
  10.  
  11. Everyone is granted permission to copy, modify and redistribute
  12. Ghostscript, but only under the conditions described in the Ghostscript
  13. General Public License.  A copy of this license is supposed to have been
  14. given to you along with Ghostscript so you can know your rights and
  15. responsibilities.  It should be in a file named COPYING.  Among other
  16. things, the copyright notice and this notice must be preserved on all
  17. copies.  */
  18.  
  19. /* zstack.c */
  20. /* Operand stack operators for Ghostscript */
  21. #include "memory_.h"
  22. #include "ghost.h"
  23. #include "errors.h"
  24. #include "oper.h"
  25. #include "store.h"
  26.  
  27. /* <obj> pop - */
  28. int
  29. zpop(register os_ptr op)
  30. {    check_op(1);
  31.     pop(1);
  32.     return 0;
  33. }
  34.  
  35. /* <obj1> <obj2> exch <obj2> <obj1> */
  36. int
  37. zexch(register os_ptr op)
  38. {    ref next;
  39.     check_op(2);
  40.     ref_assign_inline(&next, op - 1);
  41.     ref_assign_inline(op - 1, op);
  42.     ref_assign_inline(op, &next);
  43.     return 0;
  44. }
  45.  
  46. /* <obj> dup <obj> <obj> */
  47. int
  48. zdup(register os_ptr op)
  49. {    check_op(1);
  50.     push(1);
  51.     ref_assign_inline(op, op - 1);
  52.     return 0;
  53. }
  54.  
  55. /* <obj_n> ... <obj_0> <n> index <obj_n> ... <obj_0> <obj_n> */
  56. int
  57. zindex(register os_ptr op)
  58. {    register os_ptr opn;
  59.     check_type(*op, t_integer);
  60.     if ( (ulong)op->value.intval >= op - osbot )
  61.         return_error(e_rangecheck);
  62.     opn = op + ~(int)op->value.intval;
  63.     ref_assign_inline(op, opn);
  64.     return 0;
  65. }
  66.  
  67. /* <obj_n-1> ... <obj_0> <n> <i> roll */
  68. /*    <obj_(i-1)_mod_ n> ... <obj_0> <obj_n-1> ... <obj_i_mod_n> */
  69. int
  70. zroll(register os_ptr op)
  71. {    os_ptr op1 = op - 1;
  72.     int count, mod;
  73.     register os_ptr from, to;
  74.     register int n;
  75.     check_type(*op1, t_integer);
  76.     check_type(*op, t_integer);
  77.     if ( (ulong)op1->value.intval > op1 - osbot )
  78.         return_error(e_rangecheck);
  79.     count = op1->value.intval;
  80.     mod = op->value.intval;
  81.     /*
  82.      * The elegant approach, requiring no extra space, would be to
  83.      * rotate the elements in chains separated by mod elements.
  84.      * Instead, we simply check to make sure there is enough space
  85.      * above op to do the roll in two block moves.
  86.      * Unfortunately, we can't count on memcpy doing the right thing
  87.      * in *either* direction.
  88.      */
  89.     switch ( mod )
  90.     {
  91.     case 1:            /* common special case */
  92.         pop(2);  op -= 2;
  93.         for ( from = op, n = count; n--; from-- )
  94.             ref_assign(from + 1, from);
  95.         ref_assign(from + 1, op + 1);
  96.         return 0;
  97.     case -1:        /* common special case */
  98.         pop(2);  op -= 2;
  99.         to = op - count + 1;
  100.         ref_assign(op + 1, to);
  101.         for ( n = count; n--; to++ )
  102.             ref_assign(to, to + 1);
  103.         return 0;
  104.     }
  105.     if ( count <= 1 )
  106.     {    pop(2);
  107.         return 0;
  108.     }
  109.     if ( mod >= count ) mod %= count;
  110.     else if ( mod < 0 )
  111.     {    mod %= count;
  112.         if ( mod < 0 ) mod += count;  /* can't assume % means mod! */
  113.     }
  114.     if ( mod <= count >> 1)
  115.     {     /* Move everything up, then top elements down. */
  116.         if ( mod >= ostop - op )
  117.             return_error(e_stackoverflow);
  118.         pop(2);  op -= 2;
  119.         for ( to = op + mod, from = op, n = count; n--; to--, from-- )
  120.             ref_assign(to, from);
  121.         memcpy((char *)(from + 1), (char *)(op + 1), mod * sizeof(ref));
  122.     }
  123.     else
  124.     {    /* Move bottom elements up, then everything down. */
  125.         mod = count - mod;
  126.         if ( mod >= ostop - op )
  127.             return_error(e_stackoverflow);
  128.         pop(2);  op -= 2;
  129.         to = op - count + 1;
  130.         memcpy((char *)(op + 1), (char *)to, mod * sizeof(ref));
  131.         for ( from = to + mod, n = count; n--; to++, from++ )
  132.             ref_assign(to, from);
  133.     }
  134.     return 0;
  135. }
  136.  
  137. /* |- ... clear |- */
  138. /* The function name is changed, because the IRIS library has */
  139. /* a function called zclear. */
  140. int
  141. zclear_stack(os_ptr op)
  142. {    osp = osbot - 1;
  143.     return 0;
  144. }
  145.  
  146. /* |- <obj_n-1> ... <obj_0> count <obj_n-1> ... <obj_0> <n> */
  147. int
  148. zcount(register os_ptr op)
  149. {    push(1);
  150.     make_int(op, op - osbot);
  151.     return 0;
  152. }
  153.  
  154. /* - mark <mark> */
  155. int
  156. zmark(register os_ptr op)
  157. {    push(1);
  158.     make_t(op, t_mark);
  159.     return 0;
  160. }
  161.  
  162. /* <mark> ... cleartomark */
  163. int
  164. zcleartomark(register os_ptr op)
  165. {    while ( op >= osbot )
  166.        {    if ( r_has_type(op, t_mark) )
  167.            {    osp = op - 1;
  168.             return 0;
  169.            }
  170.         op--;
  171.        }
  172.     return_error(e_unmatchedmark);
  173. }
  174.  
  175. /* <mark> <obj_n-1> ... <obj_0> counttomark */
  176. /*    <mark> <obj_n-1> ... <obj_0> <n> */
  177. int
  178. zcounttomark(os_ptr op)
  179. {    register os_ptr mp = op;
  180.     while ( mp >= osbot )
  181.        {    if ( r_has_type(mp, t_mark) )
  182.            {    push(1);
  183.             make_int(op, op - mp - 1);
  184.             return 0;
  185.            }
  186.         mp--;
  187.        }
  188.     return_error(e_unmatchedmark);
  189. }
  190.  
  191. /* ------ Initialization procedure ------ */
  192.  
  193. op_def zstack_op_defs[] = {
  194.     {"0clear", zclear_stack},
  195.     {"0cleartomark", zcleartomark},
  196.     {"0count", zcount},
  197.     {"0counttomark", zcounttomark},
  198.     {"1dup", zdup},
  199.     {"2exch", zexch},
  200.     {"2index", zindex},
  201.     {"0mark", zmark},
  202.     {"1pop", zpop},
  203.     {"2roll", zroll},
  204.     op_def_end(0)
  205. };
  206.